<template>
  <div class="login-background">
    <div class="box">
      <!-- 放图像 -->
      <div class="box_img">
        <img src="../assets/pzhu.png" />
        <h1>毕业设计管理系统</h1>
      </div>
      <!-- 表单 -->
      <el-form ref="formRef" :model="loginForm" :rules="rules" class="form">
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <div class="radio">
          <el-radio v-model="loginForm.radio" label="1">学生</el-radio>
          <el-radio v-model="loginForm.radio" label="2">教师</el-radio>
          <el-radio v-model="loginForm.radio" label="3">学院管理</el-radio>
          <el-radio v-model="loginForm.radio" label="4">学校管理</el-radio>
        </div>
      </el-form>
      <!-- 按钮 -->
      <div class="button">
        <el-button type="primary" @click="login()">登录</el-button>
        <el-button type="info" @click="reset()">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单的数据绑定对象
      loginForm: {
        username: "",
        password: "",
        radio: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 6, max: 12, message: "账号在6-12之间", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "密码在6-12之间", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 重置按钮的方法
    reset() {
      // console.log(this);
      this.$refs.formRef.resetFields();
    },
    // 登录按钮的方法
    login() {
      // console.log(this.loginForm);
      if (!this.loginForm.radio) return this.$message.error("选择按钮要选择");
      // console.log(this.loginForm);

      // 学生登录------------------------------------------------------------
      if (this.loginForm.radio == 1) {
        // 对表单进行预校验
        this.$refs.formRef.validate(async (valid) => {
          if (!valid) return;
          // 发送登录请求,将返回值保存到ret中
          this.$http
            .post(
              "http://127.0.0.1:8080/api/login",
              this.$qs.stringify(this.loginForm)
            )
            .then((res) => {
              if (res.data.status != 0) {
                return this.$message.error(res.data.message);
                // return console.log("失败");
              }
              this.$message.success("欢迎光临");
              // 将登录成功后返回的token值保存在客户端的sessionStorage中
              window.sessionStorage.setItem("token", res.data.token);
              // 通过编程式导航跳转到后台主页;this.$router编程式导航对象
              this.$router.push("/index");
              // console.log(ret);
            })
            .catch((err) => {
              this.$message.error(err);
            });
        });
      }
      // 教师登录--------------------------------------------------------------
      if (this.loginForm.radio == 2) {
        this.$refs.formRef.validate((valid) => {
          if (!valid) return;
          // 发送登录请求
          this.$http
            .post(
              "http://127.0.0.1:8080/api/teacherLogin",
              this.$qs.stringify(this.loginForm)
            )
            .then((res) => {
              // console.log(res);
              if (res.data.status != 0) {
                return this.$message.error(res.data.message);
              }
              this.$message.success(res.data.message);
              window.sessionStorage.setItem("token", res.data.token);
              this.$router.push("/index_teacher");
            });
        });
      }
      // 学院管理员登录----------------------------------------------------------
      if (this.loginForm.radio == 3) {
        this.$refs.formRef.validate((valid) => {
          if (!valid) return;
          this.$http
            .post(
              "http://127.0.0.1:8080/api/minAdmin_login",
              this.$qs.stringify(this.loginForm)
            )
            .then((res) => {
              if (res.data.status != 0) {
                return this.$message.error(res.data.message);
              }
              window.sessionStorage.setItem("token", res.data.token);
              this.$router.push("/index_minAdmin");
            });
        });
      }

      // 学校管理员登录-------------------------------------------------------
      if (this.loginForm.radio == 4) {
        this.$refs.formRef.validate((valid) => {
          if (!valid) return;
          this.$http
            .post(
              "http://127.0.0.1:8080/api/bigAdmin_login",
              this.$qs.stringify(this.loginForm)
            )
            .then((res) => {
              // console.log(res);
              if (res.data.status != 0) {
                return this.$message.error(res.data.message);
              }
              window.sessionStorage.setItem("token", res.data.token);
              this.$message.success(res.data.message);
              this.$router.push("/index_bigAdmin");
            });
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-background {
  background: url("../assets/background.png"), no-repeat;
  height: 100%;
  position: relative;
}
.box {
  width: 450px;
  height: 400px;
  background-color: white;
  border-radius: 10px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.box h1 {
  color: skyblue;
}
.box .box_img {
  text-align: center;
  width: 100%;
  height: 15px;
}

.form {
  margin-top: 150px;
}

.button {
  display: flex;
  justify-content: center;
  margin: 20px 0;
}
.button button {
  width: 200px;
}
.radio {
  display: flex;
  justify-content: center;
}
</style>
